<?php
/**
 * Created by PhpStorm.
 * CeshiUser: 曹旺
 * Date: 2018-05-02
 * Time: 13:32
 */
    function getWeiXinConfig()
    {
        $options = array(
            'token' => '', // 填写你设定的key
            'encodingaeskey' => '', // 填写加密用的EncodingAESKey
            'appid' => 'wx292e1491eca7a249', // 填写高级调用功能的app id
            'appsecret' => '7dae8aa84a022ac8e160448f01523345', // 填写高级调用功能的密钥 )// 商户签名密钥Key
            'mchid' => '1493550662',
            'key' => '');
        return $options;
    }
    // php获取当前访问的完整url地址
    function GetCurUrl()
    {
        $url = 'http://';
        if (isset ($_SERVER ['HTTPS']) && $_SERVER ['HTTPS'] == 'on') {
            $url = 'https://';
        }
        if ($_SERVER ['SERVER_PORT'] != '80') {
            $url .= $_SERVER ['HTTP_HOST'] . ':' . $_SERVER ['SERVER_PORT'] . $_SERVER ['REQUEST_URI'];
        } else {
            $url .= $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI'];
        }
        // 兼容后面的参数组装
        if (stripos($url, '?') === false) {
            $url .= '?t=' . time();
        }
        return $url;
    }
    function getOpenIds()
    {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
            // $openid = session('openid');
            if (empty ($openid)) {
                include("Wechat.class.php");
                $config = getWeiXinConfig();
                $weObj = new Wechat ($config);
                $callback = GetCurUrl();
                $callback = urldecode($callback);
                $info = $weObj->getOauthAccessToken();
                // $info = $weObj->getOpenid();
                if (!$info) {
                    $url = $weObj->getOauthRedirect($callback, '', 'snsapi_base');
                    //return $url;
                    header("Location: $url");
                    exit ();
                } else {
                    // session('openid', $info ['openid']);
                    return $info ['openid'];
                }
            } else {
                return $openid;
            }
        } else {
            return '';
        }
    }